// pages/birthday/birthday.js
const app = getApp();
import util from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orgId:'',
    list:[],
    lists:[],
    pageNumber1:1,
    pageNumber2: 1,
    timeShow:false,
    initDate:"",
    limit:false,
    presentBirthInfo:"",
  },
getthismonth(){
  var that = this
  app.req({
    url: "orgmini/org/getOrgUserByTheMonthBirthday",
    data: {
      orgId: that.data.orgId,
      pageNumber:that.data.pageNumber1,
      pageSize:10,
    },
    success: function (res) {
      if (200 == res.code) {
        var list = res.data
        for (var item of list) {
          item.birthday = item.birthday.replace(/\-/g, "/")
          var date = new Date(item.birthday)
          console.log('时间',date)
          item.actTime = item.birthday.substring(5, 7) + '-' + item.birthday.substring(8, 10)
        }
        that.setData({
          list: list
        })
      }
    }
  });
},
getlists(setBir){
  var that = this
  app.req({
    url: "orgmini/org/getBirthList",
    data: {
      orgId: that.data.orgId,
      pageNumber: that.data.pageNumber2,
      pageSize: 10,
    },
    success: function (res) {
      if (200 == res.code) {
        var list = res.data.rows
        for (var item of list) {
          // if (item.birthday){
          //   item.birthday = item.birthday.replace(/\-/g, "/")
          //   var date = new Date(item.birthday)
          //   console.log('时间1', date)
          //   item.week = util.getWeek(date)
          //   item.actTime = item.birthday.substring(5, 7) + '-' + item.birthday.substring(8, 10)
          // }
        }
        if (that.data.lists && !setBir){
          list = that.data.lists.concat(list)
        }
        console.log(list)
        that.setData({
          lists: list,
          totalPages: res.data.totalPages
        })
      }
    }
  });
},
mini: function (e) {
  var that = this;
  var d = e.currentTarget.dataset;
  if (!d.item.birthday){
    return
  }
  var url = '/pages/ordinaryDetails/ordinaryDetails?id=' + d.id;
  wx.navigateToMiniProgram({
    appId: app.globalData.blessingMiniAppId,
    envVersion: app.globalData.blessingMiniEnvVersion,
    path: url,
    success(res) {
      // that.toggle("typeShow", false);
    }
  })
},
setBirthday(e){
  this.setData({
    pageNumber2: 1
  })
  var item = e.currentTarget.dataset.item
  var birthday = item.birthday.replace(/\//g, "-")
  var M = (new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1);
  var now = new Date().getFullYear() + "-" + M + "-" + new Date().getDate();
  this.setData({
    initDate: birthday || now,
    presentBirthInfo:item
  })
  setTimeout(() =>{
    this.setData({
      timeShow: true,
    })
  },0)
},
  submit: function (e) {
    console.log(e);
    console.log('日期类型：' + e.detail.dateType)
    // console.log('是否显示年份：' + e.detail.showYear)
    console.log('日期字符串：' + e.detail.dateStr)
    var birthdayDefault = "";
    var birthday ="";
    var birthday2 ="";
    var leap = "";
    var birth ="";
    var that = this;
    if (e.detail.dateType == '公历'){
      birthday = e.detail.dateStr + " 00:00:00";
      birthdayDefault = 0;
    }else{
      birthdayDefault = 1;
      birth = e.detail.dateStr.split("(");//["1990","庚午年)正月初一"];
      birthday2 = birth[1].split(")");//["庚午年","正月初一"];
      var deal = birthday2[1].split("月");//["正","初一"] ["闰五","初一"];
      console.log(birth[0], deal[0], deal[1])
      if (deal[0].indexOf("闰") == -1){
        leap = 0;
      }else{
        leap = 1;
        birthday2[1] = birthday2[1].replace("闰","")
      }
    }
    app.req({
      url:"orgmini/org/updateOrgUserBirthday",
      method:"post",
      data:{
        id: this.data.presentBirthInfo.id,
        orgId: this.data.orgId,
        birthday: birthday,
        birthdayLunar: "",
        birthdayLunarStr: birthdayDefault ? e.detail.dateStr : "",
        userId: this.data.presentBirthInfo.userId,
        leap: leap,
        birthdayDefault: birthdayDefault
      },
      success(res){
        if (res.code == 200){
          that.getlists(1);
          that.getthismonth();
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orgId: options.id,
      limit: options.limit
      // limit:true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNumber1: 1,
      pageNumber2: 1,
      list:[],
      lists:[],
    })
    this.getthismonth()
    this.getlists()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPages>=this.data.pageNumber2){
      console.log()
      this.setData({
        pageNumber2: this.data.pageNumber2 + 1
      })
      this.getlists();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})